------------------------------------------------------------------------
-- @brief 层级管理
------------------------------------------------------------------------
local P = class("LayerManager")
-- @brie 最底层
P.LayerBottom = 1
-- @brie 中间层
P.LayerMiddle = 2
-- @brie 最上层
P.LayerTop = 3
-- @brie 列表
P.LayerIndexs = {P.LayerTop, P.LayerMiddle, P.LayerBottom}
------------------------------------------------------------------------
-- @brie单例
function P.getInstance()
    if P.instance then
        return P.instance
    end
    P.instance = P.new()
    return P.instance
end
------------------------------------------------------------------------
-- @brief 构造函数
function P:ctor()

end

------------------------------------------------------------------------
-- @brief 初始化
function P:Init(mainView)
    self._layers = {{},{},{}} 
    self._ui = {}
    self._isDirty = false
    self._ui[P.LayerBottom] = mainView:getChild("bottom");
    self._ui[P.LayerMiddle] = mainView:getChild("middle");
    self._ui[P.LayerTop] = mainView:getChild("top");
end

------------------------------------------------------------------------
-- @brief 获取所有层
function P:GetLayers()
    return self._layers
end
------------------------------------------------------------------------
-- @brief 获取所有层
function P:OnDirty()
    self._isDirty = true
end
------------------------------------------------------------------------
-- @brief 获取一个层
-- @param[idx] 层级
-- @param[name] 名字
function P:GetLayer(idx, name)
    local tarLayers = self._layers[idx] or self._layers[2]
    return table.valuefunc(tarLayers, function(rhs) return name == rhs:getModelName() end)
end
------------------------------------------------------------------------
-- @brief 运行
-- @param[idx] 层级
-- @param[name] 名字
-- @param[datas] 参数
function P:GotoLayer(idx, name, params)
    local model = require(name)
    if not model then
        api.Logger:LogWarn(string.format("[LuaWarn] not found %s.", model))
        return nil
    end

    package.loaded[name] = nil
    local tarLayers = self._layers[idx] or self._layers[2]
    local tarView = self._ui[idx] or self._layers[2]
    local layer = model.new(idx, name, params)

    table.insert(tarLayers, 1, layer)
    if layer:getView() then
        tarView:AddChild(layer:getView())
    end
    return layer
end
------------------------------------------------------------------------
-- @brief 移除一个层
-- @param[idx] 层级
-- @param[name] 名字
function P:RemoveLayer(idx, key)
    local tarLayers = self._layers[idx] or self._layers[2]
    local tarView = self._ui[idx] or self._layers[2]
    local i, layer = table.ivfunc(tarLayers, function(rhs) return key == rhs:getModelName() end)
    if i ~= nil and layer ~= nil then
        layer:OnDestroy()
        table.remove(tarLayers, i)
        tarView:RemoveChild(layer:getView())
    end
end
------------------------------------------------------------------------
-- @brief 更新销毁
function P:UpdateDestory()
    if self._isDirty == false then
        return
    end
    self._isDirty = false
    for i in ipairs(P.LayerIndexs) do
        local removes = {}
        for j, layer in ipairs(self._layers[i]) do
            if layer._hasDestory then
                table.insert(removes, layer)
            end
        end
        for k, r in ipairs(removes) do
            self:removeLayer(i, r)
        end
    end    
end


------------------------------------------------------------------------
return P